#!/usr/bin/haserl --upload-limit=1048576 --upload-dir=/tmp/

<?
rm -f /tmp/firmware.img
mv $HASERL_file_path /tmp/firmware.img

echo "Content-Type: text/html; charset=utf-8"
echo ''

DOCUMENT_ROOT=$(main.sbin www_root)
lang=`main.sbin get_client_lang`
eval `cat $DOCUMENT_ROOT/apps/firmware/i18n/$lang/i18n.conf 2>/dev/null`
. $DOCUMENT_ROOT/apps/firmware/firmware_lib.sh
. $DOCUMENT_ROOT/apps/home/header.tpl.sh
firmware_size=$(ls -l /tmp/firmware.img 2>/dev/null | awk {'print $5'})
firmware_md5sum=`md5sum /tmp/firmware.img 2>/dev/null | cut -d ' ' -f 1`
md5_prefix=`echo $POST_file_name | cut -d '-' -f4`
if
echo $firmware_md5sum | grep -q "^$md5_prefix"
then
	if echo "$HTTP_USER_AGENT" | grep -q "^curl" ; then
	cat <<EOF
Content-Type: text/html; charset=utf-8

{"status": 0,"msg": "固件可用.","size": "$firmware_size","md5sum": "$firmware_md5sum"}
EOF
	else
	header_html
	cat <<EOF

		<div class="content">
			<div class="pure-g">
				<div class="l-box-lrg pure-u-1 pure-u-md-1-2">
					<p>固件可用,是否刷机?</p>
					<label>文件名:</label>
					$POST_file_name
					<label>大小:</label>
					<font color="blue">`main.sbin storage_size_conver $firmware_size`( $firmware_size )</font>
						<p>
					<label>MD5:</label>
					<font color="green">$firmware_md5sum</font>
						<form class="pure-form" action="/index.cgi?app=firmware">
							<fieldset>
								<input type="hidden" name="action" value="post_flash">
								<input type="hidden" name="app" value="firmware">
								<button type="submit" class="pure-button button-error" >刷机</button>
								<button type="button" class="pure-button pure-button-primary" onClick="location.href='/index.cgi?app=firmware'">返回</button>
							</fieldset>
						</form>
								<h3>保留文件(保存后才能生效)</h3>
EOF
	post_savefile_html
	cat <<EOF
				</div>
			</div>
		</div>
	</div>
EOF
	footer_html
	fi

else
header_html

cat <<EOF
	<div class="content">
		<div class="pure-g">
			<div class="l-box-lrg pure-u-1 pure-u-md-1-2">
				<p>固件不可用,请返回</p>
				<button type="button" class="pure-button pure-button-primary" onClick="location.href='/index.cgi?app=firmware'">返回</button>
			</div>
		</div>
	</div>
</div>
EOF
footer_html keep_bottom
fi
?>
